﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using OpenGado.DATA.Entities;


namespace OpenGado.DATA
{
    /// <summary>
    /// Class controlling DAO access for the Storage object.
    /// </summary>
    public class StorageDataControl
    {
        private static StorageDataControl storageDataControl;
        private static object lockStorageControl = new object();

        private ISession session;

        public ISession Session
        {
            get
            {
                session = FactoryHandler.CreateFactory().OpenSession();

                return session;
            }

            
        }
        /// <summary>
        /// Creates instance of Storage Data Control object
        /// </summary>
        public static StorageDataControl Instance
        {
            get
            {
                lock (lockStorageControl)
                {
                    if (storageDataControl == null)
                    {
                        storageDataControl = new StorageDataControl();
                    }

                }
                return storageDataControl;
            }
        }
        /// <summary>
        /// Persists instance of storage object to db.
        /// </summary>
        /// <param name="storage"></param>
        public void Save(Storage storage )
        {
            ISession tsession = Session;
            ITransaction transaction = tsession.BeginTransaction();
            try
            {
                tsession.SaveOrUpdate(storage);
                transaction.Commit();
            }
            catch (Exception)
            {
                //Todo: add logging here
                transaction.Rollback();
            }
            finally
            {
                tsession.Close();
            }
           
            
        }
        /// <summary>
        /// Deletes instance of Storage from db
        /// </summary>
        /// <param name="storage"></param>
        public void Delete (Storage storage)
        {
            ISession tsession = Session;
            ITransaction transaction = tsession.BeginTransaction();
            try
            {
                tsession.Delete(storage);
                transaction.Commit();
            }
            catch (Exception)
            {
                //Todo: add logging here
                transaction.Rollback();
            }
            finally
            {
                tsession.Close();
            }
           
        }

        /// <summary>
        /// Retrieves a single storage object
        /// </summary>
        /// <param name="storageId"></param>
        /// <returns>Storage object</returns>
        public Storage GetStoreById(int storageId)
        {
            ISession tsession = Session;
            Storage st =  tsession.Get<Storage>(storageId);

            tsession.Close();

            return st;
        }
        /// <summary>
        /// Retrieves a collection of all available storage locations
        /// </summary>
        /// <returns> store List</returns>
        public IList<Storage> GetAllStores()
        {
            ISession tsession = Session;
            ICriteria criteria = tsession.CreateCriteria<Storage>();
            IList<Storage> locations = criteria.List<Storage>();
            tsession.Close();

            return locations;
        }
    }
}
